//
//  WBMainViewController.swift
//  WeiBo
//
//  Created by zqq on 2017/7/20.
//  Copyright © 2017年 qq. All rights reserved.
//

import UIKit



/// 主控制器
class WBMainViewController: UITabBarController {
    lazy var composeButton: UIButton = UIButton.cz_imageButton("tabbar_compose_icon_add", backgroundImageName: "tabbar_compose_button")
    
    override func viewDidLoad() {
        super.viewDidLoad()

       setupChildControllers()
        setupComposeButton()
    }
    
    
    @objc func composeStatus(){
        print("撰写微博")
    }
}

extension WBMainViewController{
    
   
    
    func setupComposeButton(){
        tabBar.addSubview(composeButton)
        
        let count = CGFloat(childViewControllers.count)
        
        let w = CGFloat(tabBar.bounds.width / count) - 1
        
        composeButton.frame = tabBar.bounds.insetBy(dx: 2 * w, dy: 0)
        
        composeButton.addTarget(self, action: #selector(composeStatus), for: .touchUpInside)
    }
    
     func setupChildControllers(){
        let arrayDict = [
            ["clsName":"WBHomeController","title": "首页", "imageName": "home"],
            ["clsName":"WBMessageController","title": "消息", "imageName": "message_center"],
            ["clsName":"UIViewController"],
            ["clsName":"WBDiscoverController","title": "发现", "imageName": "discover"],
            ["clsName":"WBProfileController","title": "我", "imageName": "profile"],
        
        ]
        var arrayM = [UIViewController]()

        for dict in arrayDict {
            arrayM.append(controller(dict: dict))
        }
        
        viewControllers = arrayM
    }
    
    // dict:信息字典 [clsName, title, imageName]
    private func controller(dict:[String: String]) -> UIViewController{
        // 1.取得字典内容
        
        guard let clsName = dict["clsName"]
            else{
            return UIViewController()
        }
        
     
        
        guard let title = dict["title"] as? String
            else {
            return UIViewController()

        }
        guard let imageName = dict["imageName"] as? String
            else {
                return UIViewController()
        }
        

        
        guard let cls  = NSClassFromString(Bundle.main.namespace + "." + clsName) as? UIViewController.Type
            
            else {
                return UIViewController()
                
         }
        
        // 2 创建视图控制器
        
        let vc = cls.init()
        vc.title = title
        
        vc.tabBarItem.image = UIImage(named: "tabbar_" + imageName)
        vc.tabBarItem.selectedImage = UIImage(named: "tabbar_" + imageName + "_selected")?.withRenderingMode(.alwaysOriginal)
        
        vc.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.orange], for: .highlighted)
        
        vc.tabBarItem.setTitleTextAttributes([NSFontAttributeName: UIFont.systemFont(ofSize: 12)], for: UIControlState(rawValue: 0))
        
        let nav = WBNavigationController(rootViewController: vc)
        
        return nav
        
    }
}
